// Bachelor of Software Engineering 
// Media Design School 
// Auckland
// New Zealand
// 
// (c) 2005 - 2014 Media Design School 
// 
// File Name : BackBuffer.cpp
// Description : BackBuffer implementation
// Author : Serge Radinovich
// Mail : srad015@hotmail.com

#pragma once

#include "BackBuffer.h"
/////////////////////////////
//Default constructor
//Create DC compatible to main window for back buffer
/////////////////////////////
BackBuffer::BackBuffer(HWND _hWnd, float _iWidth, float _iHeight)
{
	m_hWnd = _hWnd;
	HDC _hWndDC = GetDC(_hWnd);

	m_iWidth = static_cast<int>(_iWidth);
	m_iHeight = static_cast<int>(_iHeight);

	m_hDC = CreateCompatibleDC(_hWndDC);

	m_hSurface = CreateCompatibleBitmap(_hWndDC, static_cast<int>(_iWidth), static_cast<int>(_iHeight));

	ReleaseDC(_hWnd, _hWndDC);

	m_hOldObject = (HBITMAP)SelectObject(m_hDC, m_hSurface);

	HBRUSH white = (HBRUSH)GetStockObject(WHITE_BRUSH);
	HBRUSH oldBrush = (HBRUSH)SelectObject(m_hDC, white);

	Rectangle(m_hDC, 0,0, m_iWidth, m_iHeight);
	SelectObject(m_hDC, oldBrush);

}

/////////////////////////////
//Destructor
/////////////////////////////
BackBuffer::~BackBuffer()
{
	SelectObject(m_hDC, m_hOldObject);
	DeleteObject(m_hSurface);
	DeleteDC(m_hDC);
}

/////////////////////////////
//Returns DC of back buffer, used for drawing
/////////////////////////////
HDC BackBuffer::getDC()
{
	return m_hDC;
}

/////////////////////////////
//Returns old object of BB
/////////////////////////////
HBITMAP BackBuffer::getOldObject()
{
	return m_hOldObject;
}

/////////////////////////////
//Returns width of BB
/////////////////////////////
int BackBuffer::Width()
{
	return m_iWidth;
}

/////////////////////////////
//Returns height of BB
/////////////////////////////
int BackBuffer::Height()
{
	return m_iHeight;
}

/////////////////////////////
//Presents the back buffer to window
/////////////////////////////
void BackBuffer::Present()
{
	HDC _hWndDC = GetDC(m_hWnd);

	BitBlt(_hWndDC, 0, 0, m_iWidth, m_iHeight, m_hDC, 0, 0, SRCCOPY);

	ReleaseDC(m_hWnd, _hWndDC);
}

/////////////////////////////
//Sets BB background DC
/////////////////////////////
void BackBuffer::SetBackgroundDC(HDC _hDC)
{
	m_hBackgroundDC = _hDC;
}

/////////////////////////////
//Resets back buffer to above mentioned background
/////////////////////////////
void BackBuffer::Clear()
{
	BitBlt(m_hDC, 0, 0, m_iWidth, m_iHeight, m_hBackgroundDC, 0, 0, SRCCOPY);
}